import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex);
const store =new Vuex.Store({
    state: {
        cart:[
            {
                goodid:'chenzihong',
                imgList:['http://img2.ch999img.com/pic/product/800x800/20170227141028301.jpg'],
                count:3,
                nowPrice:123,
                title:'我是测试数据我是测试数据我是测试数据我是测试数据',
                value:true,
            },
           
        ],
        islogin:false,
        goodslength:0,
        total:0,
        value:[],
        allcheck:true
    },
    getters:{
        cart: state => state.cart,    //购物车数据
        goodslength: state => { //购物车商品的数量
            var num =0
            state.cart.forEach(function(ele,index) {
                num += ele.count
            });
            return num
        },
        islogin: state => state.islogin,    //是否登录了
        total:state => {    //商品总金额
            var alltotal = 0
            state.cart.forEach(function(ele,index) {
                if(ele.value){
                    alltotal += ele.nowPrice * ele.count
                }
            });
            return alltotal
        },
        //监测各个商品是否勾选
        value:state => {
            var valuearr =[]
            
            state.cart.forEach(function(ele,index) {
                //把购物车中的物品的value添加给一个新数组
                valuearr.push(ele.value)
                //计算选中商品的true or false
                if(!ele.value){
                    state.allcheck = false
                }
            });
            return valuearr
        },
        //监测全选按钮
        allcheck:state => {
            //foreach不能跳出循环
            // state.value.forEach(function(val,index) {
            //     state.allcheck = true
            //     if(!val){
            //         state.allcheck = false
            //         break
            //     }
            // });
            for(var i=0;i<state.value.length;i++){
                state.allcheck = true
                if(!state.value[i]){
                    state.allcheck = false
                    break
                }
            }
            return state.allcheck
        }
        
    },
    mutations:{
        //添加商品
        addToList(state,item){
            //判读商品是否有重复
            var flag =false
            state.cart.forEach(function(ele,index) {
                if(ele.goodid===item.goodid){
                    flag=true
                }
            });
            //不重复就添加进商品数组里面
            if(!flag){
                state.cart.push(item)
            }
        },
        //商品个数的增加
        addnum(state,itemid){
            state.cart.forEach(function(ele,index) {
                if(ele.goodid===itemid){
                    ele.count++
                }
            });
            //由于只改变数组里面的对象没直接修改数组所以视图并不会更新。要修改一下数组才会更新
            var a = state.cart
            state.cart = []
            state.cart = a
            a = null
        },
        //商品个数的减少
        subtract(state,itemid){
            state.cart.forEach(function(ele,index) {
                if(ele.goodid===itemid){
                    if(ele.count>1){
                        ele.count--
                    }
                }
            });
            //由于只改变数组里面的对象没直接修改数组所以视图并不会更新。要修改一下数组才会更新
            var a = state.cart
            state.cart = []
            state.cart = a
            a = null
        },
        //删除
        del(state,itemid){
            state.cart.forEach(function(ele,index) {
                //删除购物车的种类
                if(ele.goodid===itemid){
                     state.cart.splice(index,1)
                }
            });
        },
        //改变数组value的true or false 值
        changeval(state,item){
            state.value = item.valuearr
            state.cart[item.index].value = !state.cart[item.index].value
            //由于只改变数组里面的对象没直接修改数组所以视图并不会更新。要修改一下数组才会更新
            var a = state.cart
            state.cart = []
            state.cart = a
            a = null
        },
        //全选与取反
        NOT(state){
            state.allcheck = !state.allcheck
            state.cart.forEach(function(ele,index) {
                //购物车商品取反
                ele.value = state.allcheck
                state.value[index] =state.allcheck
            });
            var a = state.cart
            state.cart = []
            state.cart = a
            a = null
        }
    },
    
})
export default store